/*
 * @Author: zhaiyujia
 * @Date: 2022-11-16 00:43:31
 * @LastEditors: zhaiyujia
 * @LastEditTime: 2022-11-17 15:00:47
 * @Description: ProtoThreads test.
 */

#include "Arduino.h"
#include "pt/pt.h"

static int protothread1_flag, protothread2_flag;

#define LED D55

static int protothread1(struct pt *pt)
{
    PT_BEGIN(pt);
    while (1)
    {
        PT_WAIT_UNTIL(pt, protothread2_flag != 0);
        Serial_println("Protothread 1 running.");
        protothread2_flag = 0;
        protothread1_flag = 1;
    }
    PT_END(pt);
}

static int protothread2(struct pt *pt)
{
    PT_BEGIN(pt);
    while (1)
    {
        protothread2_flag = 1;
        PT_WAIT_UNTIL(pt, protothread1_flag != 0);
        Serial_println("Protothread 2 running.");
        protothread1_flag = 0;
    }
    PT_END(pt);
}

static struct pt pt1, pt2;

void setup()
{
    pinMode(LED, OUTPUT);
    Serial_begin(115200);
    Serial_println("Hello Arduino-STC.");
    PT_INIT(&pt1);
    PT_INIT(&pt2);
}
void loop()
{
    // digitalWrite(LED, !digitalRead(LED));
    // delay(500);
    // Serial_println("Hello Arduino-STC.");

    protothread1(&pt1);
    protothread2(&pt2);
}